package ru.javadevelop.vegetarian.model;

import ru.javadevelop.vegetarian.common.DB;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class Recipe {
    public int id;
    public String addedByUser;
    public int categoryId;
    public Date date;
    public String ingredients;
    public String name;
    public String process;
    public int raiting;

    //******************************************************************************************************************
    public Recipe (int id, String addedByUser, int categoryId, Date date, String ingredients,
                   String name, String process, int raiting)
    {
        this.id = id;
        this.addedByUser = addedByUser;
        this.categoryId = categoryId;
        this.date = date;
        this.ingredients = ingredients;
        this.name = name;
        this.process = process;
        this.raiting = raiting;

    }

    //********************************************************************************************************
    public int save() throws SQLException {
        boolean exists = this.id != 0 && DB.INSTANCE.executeSql(new DB.Handler<Boolean>() {
            @Override
            public Boolean  execute(Statement stmt) throws SQLException {
                ResultSet resultSet = stmt.getResultSet();
                return resultSet.next();
            }
        }, "SELECT * FROM Recipe WHERE id=?", this.id);
        // Если запись существовала, — обновляем её  (проверяется по id)
        // у вновь создаваемых записей id=0

        if  (exists)
        {
            DB.INSTANCE.executeSql("UPDATE Recipe SET addedByUser = ?, categoryId = ?, date_=?," +
                                   " ingredients=?, name=?, process=?, raiting=?  WHERE id = ?",
                                    this.addedByUser, this.categoryId, this.date,
                                    this.ingredients, this.name, this.process, this.raiting, this.id);
            return this.id;
        }
        else
        {
            DB.INSTANCE.executeSql("INSERT INTO Recipe (id, addedByUser, categoryId, date_, " +
                                   "ingredients, name, process, raiting ) VALUES(?,?,?,?,?,?,?,?)",
                                   this.id, this.addedByUser, this.categoryId, this.date,
                                   this.ingredients, this.name, this.process, this.raiting);
            return this.id;
        }
    }
    //******************************************************************************************************************
    public void delete() {
        DB.INSTANCE.executeSql("DELETE FROM Recipe WHERE id=?", this.id);
    }

    //********************************************************************************************************
    public static Recipe getById(int id) {
        return DB.INSTANCE.executeSql(new DB.Handler<Recipe>() {
            @Override
            public Recipe execute(Statement stmt) throws SQLException {
                ResultSet resultSet = stmt.getResultSet();
                resultSet.next();
                return  new Recipe(
                        resultSet.getInt("id"),
                        resultSet.getString("addedByUser"),
                        resultSet.getInt("categoryId"),
                        resultSet.getDate("date_"),
                        resultSet.getString("ingredients"),
                        resultSet.getString("name"),
                        resultSet.getString("process"),
                        resultSet.getInt("raiting")
                );
            }
        }, "SELECT * FROM recipe WHERE id=?", id);
    }

    //********************************************************************************************************
    public static ArrayList<Recipe> getAll()
    {
        return DB.INSTANCE.executeSql(new DB.Handler<ArrayList<Recipe>>() {
            @Override
            public ArrayList<Recipe> execute(Statement stmt) throws SQLException {
                ArrayList<Recipe> result = new ArrayList<Recipe>();
                ResultSet resultSet = stmt.getResultSet();
                while (resultSet.next())
                {
                    result.add(new Recipe(
                            resultSet.getInt("id"),
                            resultSet.getString("addedByUser"),
                            resultSet.getInt("categoryId"),
                            resultSet.getDate("date_"),
                            resultSet.getString("ingredients"),
                            resultSet.getString("name"),
                            resultSet.getString("process"),
                            resultSet.getInt("raiting"))
                    );
                }
                return result;

            }
        }, "SELECT * FROM recipe");
    }
    //******************************************************************************************************************
    public static ArrayList<Recipe> getByCategory(int categoryId) {
        return DB.INSTANCE.executeSql(new DB.Handler<ArrayList<Recipe>>() {
            @Override
            public ArrayList<Recipe> execute(Statement stmt) throws SQLException {
                ArrayList<Recipe> result = new ArrayList<Recipe>();
                ResultSet resultSet = stmt.getResultSet();
                while (resultSet.next())
                {
                    result.add(new Recipe(
                            resultSet.getInt("id"),
                            resultSet.getString("addedByUser"),
                            resultSet.getInt("categoryId"),
                            resultSet.getDate("date_"),
                            resultSet.getString("ingredients"),
                            resultSet.getString("name"),
                            resultSet.getString("process"),
                            resultSet.getInt("raiting"))
                    );
                }
                return result;

            }
        }, "SELECT * FROM recipe WHERE categoryId=?", categoryId);
    }

    //******************************************************************************************************************
    //******************************************************************************************************************
    //****************  Навигационные методы ***************************************************************************
    public ArrayList<Image> getImages()
    {
        return Image.getByRecipe(this.id);
    }
    //******************************************************************************************************************
    public List<Translation> getTranslations()
    {
        return null;
    }
    //******************************************************************************************************************
    public Category getCategory()
    {
        return Category.getById(this.categoryId);
    }
    //******************************************************************************************************************
    @Override
    public boolean equals(Object recipe)
    {
        Recipe rc;
        try{
            rc = (Recipe) recipe;
        }
        catch (ClassCastException e)
        {
            return false;
        }
        return (    (rc.id==this.id) &&
                    (rc.addedByUser.equals(this.addedByUser)) &&
                    (rc.date==this.date) );


    }

    public static ArrayList<Recipe> getFavorites() {
        return null;
    }
    //******************************************************************************************************************

}
